<?php 
/**
 * 领取破产救济金
 * GetBrokeAlms
 */

class GetBrokeAlms extends APIBase {
	public $tag = "GetBrokeAlms";

	public function before() {
		$this->initMysql();
		return true;
	}

	public function logic() {
		$isExists = $this->data_redis->exists("hu:{$this->uid}");
		if (!$isExists){
			$this->returnError(302,'user is not exists');
		}

		$player = $this->data_redis->hMGet("hu:{$this->uid}", array('id','money','bank', 'broke_num', 'broke_time'));
		if ($player['id'] == null){
			$this->returnError(302,'redis error');

		}

		$broke_num = (int)$player['broke_num'];
		$broke_time = (int)$player['broke_time'];
		$broke_give_money = Game::$broke_give_money;

		if ((int)$player['money'] + (int)$player['bank'] >=  Game::$broke_money) {
			$this->returnError(301, 'you money is not meet the conditions to get alms.');
		}
			
		$timestamp = strtotime(date("Y-m-d"));
		if ($broke_time < $timestamp or $broke_num < Game::$broke_num_max) {
			$money = $this->data_redis->hincrBy("hu:{$this->uid}", "money", $broke_give_money);
			if ($broke_time < $timestamp) {
				$this->data_redis->hset("hu:{$this->uid}", "broke_num", 1);
			} else {
				$this->data_redis->hincrBy("hu:{$this->uid}", "broke_num", 1);
			}
			$this->data_redis->hset("hu:{$this->uid}", "broke_time", time());
			$broke_info = array();
			$broke_info['money'] = $money;
			$broke_info['broke_give_money'] = $broke_give_money;
			
			//领取救济金
			$m_rec['type'] = 2;
			$m_rec['uid']  =  $this->uid;
			$m_rec['money'] = (int)$player['money'] + (int)$broke_give_money;
			$m_rec['change_money']  =  (int)$broke_give_money;
			$this->addChangeMongeyLog($m_rec);
			
			$this->returnData($broke_info);
		} else {
			$this->returnError(302, 'you have get the alms all already today.');
		}
	}

	public function after() {
		$this->deinitMysql();
		$this->deinitDataRedis();
	}

}

?>



